package com.TIEmulator;

import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Spinner;
import com.TIEmulator.FileIOLib.FileIOLib;
import java.util.Iterator;
import java.util.List;

public class TIEmuStartup extends MyAct
  implements AdapterView.OnItemSelectedListener, DialogInterface.OnDismissListener, AssetHTMLView.HTMLViewerCb, SDCardPoll.OnChangeCallback
{
  private static String SD_CARD_PATH;
  protected static final int SUB_ACTIVITY_BROWSER_REQUEST_CODE = 0;
  protected static final int SUB_ACTIVITY_DOWNMAN_REQUEST_CODE = 1;
  private static final String gpl_rom_asset_dir = "gplroms/";
  private static final String[][] gpl_roms;
  private static final String keymap_asset_dir = "keymaps/";
  private static final String[] keymap_files;
  private ArrayAdapter<String> mAdapterROMSpinner = null;
  private TIEmulatorSettings tiemu_setup = null;

  static
  {
    String[][] arrayOfString; = new String[4][];
    String[] arrayOfString1 = new String[2];
    arrayOfString1[0] = "9x GPL'ed OS";
    arrayOfString1[1] = "pedrom9x.tib";
    arrayOfString;[0] = arrayOfString1;
    String[] arrayOfString2 = new String[2];
    arrayOfString2[0] = "89 GPL'ed OS";
    arrayOfString2[1] = "pedrom89.tib";
    arrayOfString;[1] = arrayOfString2;
    String[] arrayOfString3 = new String[2];
    arrayOfString3[0] = "89t GPL'ed OS";
    arrayOfString3[1] = "pedrom89t.tib";
    arrayOfString;[2] = arrayOfString3;
    String[] arrayOfString4 = new String[2];
    arrayOfString4[0] = "v2 GPL'ed OS";
    arrayOfString4[1] = "pedromv2.tib";
    arrayOfString;[3] = arrayOfString4;
    gpl_roms = arrayOfString;;
    String[] arrayOfString5 = new String[2];
    arrayOfString5[0] = "ti92.map";
    arrayOfString5[1] = "ti89.map";
    keymap_files = arrayOfString5;
    SD_CARD_PATH = "/sdcard";
  }

  private void onFirstStartup()
  {
    String[] arrayOfString1 = keymap_files;
    int i = arrayOfString1.length;
    int j = 0;
    String[][] arrayOfString;
    int k;
    if (j >= i)
    {
      arrayOfString = gpl_roms;
      k = arrayOfString.length;
    }
    for (int l = 0; ; ++l)
    {
      if (l >= k)
      {
        return;
        String str = arrayOfString1[j];
        FileIOLib.copyAssetFileToPkgDir(this, "keymaps/" + str);
        ++j;
      }
      String[] arrayOfString2 = arrayOfString[l];
      FileIOLib.copyAssetFileToPkgDir(this, "gplroms/" + arrayOfString2[1]);
    }
  }

  private void setupView()
  {
    setContentView((ScrollView)View.inflate(this, 2130903049, null));
    Spinner localSpinner = (Spinner)findViewById(2131296320);
    if (localSpinner == null)
      Log.e(super.toString(), "Error Find View by ID did not return a Valid View for Spinner!");
    this.mAdapterROMSpinner = new ArrayAdapter(this, 17367048);
    if (this.mAdapterROMSpinner == null)
      Log.e(super.toString(), "Error createing ROM Spinner String adapter!");
    this.mAdapterROMSpinner.setDropDownViewResource(17367049);
    localSpinner.setAdapter(this.mAdapterROMSpinner);
    localSpinner.setOnItemSelectedListener(this);
    if (Environment.getExternalStorageState().equals("mounted"))
      ((CheckBox)findViewById(2131296317)).setChecked(this.tiemu_setup.getUseSD());
    while (true)
    {
      ((CheckBox)findViewById(2131296318)).setChecked(this.tiemu_setup.getPossesTI());
      ((EditText)findViewById(2131296323)).setText(this.tiemu_setup.getCurrDir());
      findViewById(2131296324).setOnClickListener(this);
      findViewById(2131296263).setOnClickListener(this);
      findViewById(2131296327).setOnClickListener(this);
      findViewById(2131296326).setOnClickListener(this);
      findViewById(2131296317).setOnClickListener(this);
      findViewById(2131296318).setOnClickListener(this);
      updateView();
      return;
      onSdcardStateChanged(false);
    }
  }

  private void spinnerSelectCurrendFnSet()
  {
    String str = this.tiemu_setup.getCurrRomFn();
    int i = this.mAdapterROMSpinner.getCount();
    for (int j = 0; ; ++j)
    {
      if (j >= i);
      while (true)
      {
        return;
        if (!(((String)this.mAdapterROMSpinner.getItem(j)).equals(str)))
          break;
        ((Spinner)findViewById(2131296320)).setSelection(j);
      }
    }
  }

  private void startTIEmulatorActivity()
  {
    startActivity(new Intent(this, TIEmulator.class));
  }

  private void updateView()
  {
    boolean bool1 = ((CheckBox)findViewById(2131296317)).isChecked();
    boolean bool2 = validatePath();
    boolean bool3;
    label28: Button localButton;
    boolean bool4;
    label89: int j;
    label111: label118: View localView1;
    if ((bool1) && (!(bool2)))
    {
      bool3 = false;
      Spinner localSpinner = (Spinner)findViewById(2131296320);
      CheckBox localCheckBox = (CheckBox)findViewById(2131296318);
      localButton = (Button)findViewById(2131296263);
      bool4 = localCheckBox.isChecked();
      if (!(bool1))
        break label198;
      if (bool2)
        break label170;
      this.mAdapterROMSpinner.clear();
      localButton.setEnabled(false);
      localButton.setText(2131165226);
      View localView2 = findViewById(2131296321);
      if (!(bool2))
        break label191;
      j = 0;
      localView2.setVisibility(j);
      localSpinner.setEnabled(bool3);
      if (bool3)
        spinnerSelectCurrendFnSet();
      localView1 = findViewById(2131296321);
      if (!(bool1))
        break label222;
    }
    for (int i = 0; ; i = 8)
    {
      localView1.setVisibility(i);
      findViewById(2131296326).setEnabled(bool2);
      return;
      bool3 = true;
      break label28:
      label170: useRomDir(this.tiemu_setup.getCurrDir());
      localButton.setEnabled(bool4);
      break label89:
      label191: j = 8;
      break label111:
      label198: useInternalRomDir();
      localButton.setEnabled(bool4);
      localButton.setText(2131165225);
      bool2 = true;
      label222: break label118:
    }
  }

  private void useInternalRomDir()
  {
    useRomDir(FileIOLib.GetBaseDir() + "/" + "gplroms/");
  }

  private void useRomDir(String paramString)
  {
    if (this.mAdapterROMSpinner == null);
    label7: List localList;
    do
    {
      return;
      ((EditText)findViewById(2131296323)).setText(paramString);
      this.mAdapterROMSpinner.clear();
      this.tiemu_setup.setCurrDir(paramString);
      localList = FileIOLib.searchImgFilesInPath(paramString);
    }
    while (localList == null);
    Iterator localIterator = localList.iterator();
    label52: Spinner localSpinner;
    if (!(localIterator.hasNext()))
    {
      localSpinner = (Spinner)findViewById(2131296320);
      if (this.mAdapterROMSpinner.getCount() <= 0)
        break label118;
    }
    for (boolean bool = true; ; bool = false)
    {
      localSpinner.setClickable(bool);
      break label7:
      String str = (String)localIterator.next();
      this.mAdapterROMSpinner.add(str);
      label118: break label52:
    }
  }

  private boolean validatePath()
  {
    String str = this.tiemu_setup.getFnPath();
    if (str.substring(0, SD_CARD_PATH.length()).equals(SD_CARD_PATH));
    for (boolean bool = FileIOLib.validateFnPath(str); ; bool = false)
      return bool;
  }

  public void onAccept()
  {
    setupView();
  }

  protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent)
  {
    super.onActivityResult(paramInt1, paramInt2, paramIntent);
    if (paramIntent != null)
    {
      String str = paramIntent.getExtras().getString("1") + "/";
      this.tiemu_setup.setCurrDir(str);
      useRomDir(str);
      if (this.mAdapterROMSpinner.getCount() > 0)
        this.tiemu_setup.setCurrRomFn((String)this.mAdapterROMSpinner.getItem(0));
    }
    updateView();
  }

  public void onClick(View paramView)
  {
    super.onClick(paramView);
    switch (paramView.getId())
    {
    default:
    case 2131296263:
    case 2131296326:
    case 2131296324:
    case 2131296317:
    case 2131296318:
    }
    while (true)
    {
      return;
      startActivityForResult(new Intent(this, DownloadManager.class), 1);
      continue;
      if (!(this.tiemu_setup.WriteNativeSettings()))
        continue;
      startTIEmulatorActivity();
      continue;
      Intent localIntent = new Intent(this, TIEmuBrowser.class);
      localIntent.putExtra("0", this.tiemu_setup.getCurrDir());
      startActivityForResult(localIntent, 0);
      continue;
      this.tiemu_setup.setUseSD(((CheckBox)paramView).isChecked());
      updateView();
      continue;
      this.tiemu_setup.setPossesTI(((CheckBox)paramView).isChecked());
      updateView();
    }
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    new SDCardPoll(this);
    this.tiemu_setup = new TIEmulatorSettings(this);
    FileIOLib.setupFromMain(this);
    if (this.tiemu_setup.getStartupCount() == 0)
      onFirstStartup();
    this.tiemu_setup.incStartupCount();
    if (!(Eula.show(this)))
      return;
    onAccept();
  }

  public void onDismiss(DialogInterface paramDialogInterface)
  {
    findViewById(2131296326).setEnabled(true);
    findViewById(2131296263).setVisibility(4);
  }

  public void onItemSelected(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
  {
    String str = paramAdapterView.getItemAtPosition(paramInt).toString();
    this.tiemu_setup.setCurrRomFn(str);
    updateView();
  }

  public void onNothingSelected(AdapterView<?> paramAdapterView)
  {
  }

  public void onSdcardStateChanged(boolean paramBoolean)
  {
    Log.i("SDCardPoll", "sd mount state changed: " + paramBoolean);
    CheckBox localCheckBox = (CheckBox)findViewById(2131296317);
    if (localCheckBox == null)
      label38: return;
    if ((localCheckBox.isChecked()) && (!(paramBoolean)))
    {
      this.tiemu_setup.setUseSD(false);
      localCheckBox.setChecked(false);
    }
    if (!(paramBoolean))
      localCheckBox.setEnabled(false);
    while (true)
    {
      updateView();
      break label38:
      localCheckBox.setEnabled(true);
    }
  }
}
